//
//  PriceView.m
//  EasyToo
//
//  Created by hydb on 13-6-12.
//
//

#import "PriceView.h"


@implementation PriceView
//{
//    UILabel *_descLabel;
//    UILabel *_airCompanyLabel;
//    UILabel *_startTimeLabel;
//    UILabel *_startPortLabel;
//    UILabel *_arriveTimeLabel;
//    UILabel *_arrivePortLabel;
//    UILabel *_discountPriceLabel;
//    UILabel *_allPriceLabel;
//}
//
//- (void)dealloc
//{
//    [_airCompanyLabel release];
//    [_startTimeLabel release];
//    [_startPortLabel release];
//    [_arriveTimeLabel release];
//    [_arrivePortLabel release];
//    [_discountPriceLabel release];
//    [_allPriceLabel release];
//    
//    _descLabel = nil;
//    _airCompanyLabel = nil;
//    _startTimeLabel = nil;
//    _startPortLabel = nil;
//    _arriveTimeLabel = nil;
//    _arrivePortLabel = nil;
//    _discountPriceLabel = nil;
//    _allPriceLabel = nil;
//    
//    [super dealloc];
//}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UITableView* tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        [self addSubview:tableView];
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        [tableView setFrame:CGRectMake(0, 0, width, height)];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        self.tableview = tableView;
        
    }
    return self;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.airArray count] + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
    
    // 列寬
    CGFloat contentWidth = self.frame.size.width;
    
    if (indexPath.row == 0) {
        UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, contentWidth, 30)];
        descLabel.text = [NSString stringWithFormat:@"%@  %@ -> %@", self.startDate, self.startCity, self.arriveCity];
        descLabel.font = [UIFont systemFontOfSize:15];
        descLabel.backgroundColor = [UIColor grayColor];
        descLabel.textAlignment = UITextAlignmentCenter;
        [cell.contentView addSubview:descLabel];
        [descLabel release];
    } else {
        
        NSInteger row = [indexPath row] - 1;
        
        OrderAirInfo *orderInfo = [self.airArray objectAtIndex:row];
        //航空公司
        UILabel *abairCompanyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 25)];
        abairCompanyLabel.text = [NSString stringWithFormat:@"航班：%@", orderInfo.fltno];
        abairCompanyLabel.font = [UIFont systemFontOfSize:12];
        abairCompanyLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:abairCompanyLabel];
        [abairCompanyLabel release];
        
        //起飞时间
        UILabel *startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 50, 20)];
        startTimeLabel.text = orderInfo.deptime;
        startTimeLabel.font = [UIFont systemFontOfSize:12];
        startTimeLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:startTimeLabel];
        [startTimeLabel release];
        
        //起飞机场
        UILabel *startPortLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 35, 120, 20)];
        startPortLabel.text = orderInfo.scAirdrome;
        startPortLabel.font = [UIFont systemFontOfSize:12];
        startPortLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:startPortLabel];
        [startPortLabel release];
        
        //到达时间
        UILabel *arriveTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 50, 20)];
        arriveTimeLabel.text = orderInfo.arrtime;
        arriveTimeLabel.font = [UIFont systemFontOfSize:12];
        arriveTimeLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:arriveTimeLabel];
        [arriveTimeLabel release];
        
        //到达机场
        UILabel *arrivePortLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 55, 120, 20)];
        arrivePortLabel.text = orderInfo.ecAirdrome;
        arrivePortLabel.font = [UIFont systemFontOfSize:12];
        arrivePortLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:arrivePortLabel];
        [arrivePortLabel release];
        
        //折扣价
        UILabel *discountPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 15, 120, 30)];
        discountPriceLabel.text = [NSString stringWithFormat:@"￥%@", orderInfo.saleprice];
        discountPriceLabel.font = [UIFont systemFontOfSize:22];
        discountPriceLabel.textColor = [UIColor redColor];
        discountPriceLabel.textAlignment = UITextAlignmentRight;
        discountPriceLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:discountPriceLabel];
        [discountPriceLabel release];
        
        //机建费
        UILabel *allPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 50, 120, 20)];
        allPriceLabel.text = [NSString stringWithFormat:@"机建费：￥%@", orderInfo.buildfee];
        allPriceLabel.font = [UIFont systemFontOfSize:12];
        allPriceLabel.textColor = [UIColor grayColor];
        allPriceLabel.textAlignment = UITextAlignmentRight;
        allPriceLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:allPriceLabel];
        [allPriceLabel release];
        
        //燃油附加费
        UILabel *oldAddPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 70, 120, 20)];
        oldAddPriceLabel.text = [NSString stringWithFormat:@"燃烧附加费：￥%@", orderInfo.fuelfee];
        oldAddPriceLabel.font = [UIFont systemFontOfSize:12];
        oldAddPriceLabel.textColor = [UIColor grayColor];
        oldAddPriceLabel.textAlignment = UITextAlignmentRight;
        oldAddPriceLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:oldAddPriceLabel];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 30;
    } else {
        return 90;
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    cell.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:252.0/255.0 alpha:1.0];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] != 0) {
        NSInteger row = [indexPath row] - 1;
        [self.delegate onClickTableItem:[self.airArray objectAtIndex:row]];
    }

}

@end
